#include "sysset.h"

SysSet::SysSet(QWidget *parent) : QWidget(parent)
{
    this->setMinimumSize(800,480);
    this->setMaximumSize(800,480);

    int y = 90;
    int x = 230;

    line1 = new QLabel(this);
    line1->setAlignment(Qt::AlignHCenter);
    line1->setGeometry(QRect(x, y, 500, 10));
    line1->setPixmap(QPixmap(":/images/line.png"));


    line2 = new QLabel(this);
    line2->setAlignment(Qt::AlignHCenter);
    line2->setGeometry(QRect(x, y+80, 500, 10));
    line2->setPixmap(QPixmap(":/images/line.png"));


    line3 = new QLabel(this);
    line3->setAlignment(Qt::AlignHCenter);
    line3->setGeometry(QRect(x, y+160, 500, 10));
    line3->setPixmap(QPixmap(":/images/line.png"));

    cityIcon = new QLabel(this);
    cityIcon->setAlignment(Qt::AlignHCenter);
    cityIcon->setGeometry(QRect(x, y+10, 50, 50));
    cityIcon->setPixmap(QPixmap(":/images/city_icon.png"));
    cityIcon->setScaledContents(true);

    netIcon = new QLabel(this);
    netIcon->setAlignment(Qt::AlignHCenter);
    netIcon->setGeometry(QRect(x, y+90, 50, 50));
    netIcon->setPixmap(QPixmap(":/images/timer_icon.png"));
    netIcon->setScaledContents(true);

    refreshIcon = new QLabel(this);
    refreshIcon->setAlignment(Qt::AlignHCenter);
    refreshIcon->setGeometry(QRect(x, y+175, 50, 50));
    refreshIcon->setPixmap(QPixmap(":/images/update_icon.png"));
    refreshIcon->setScaledContents(true);

    cityName = new ShowLabel(this);
    cityName->addFontSize(2);
    cityName->setAlignment(Qt::AlignHCenter);
    cityName->setGeometry(QRect(320,y+30,160,60));
    cityName->setText(tr("城市设置"));

    netName = new ShowLabel(this);
    netName->addFontSize(2);
    netName->setAlignment(Qt::AlignHCenter);
    netName->setGeometry(QRect(320,y+110,160,60));
    netName->setText(tr("网络更新间隔"));

    refreshName = new ShowLabel(this);
    refreshName->addFontSize(2);
    refreshName->setAlignment(Qt::AlignHCenter);
    refreshName->setGeometry(QRect(320,y+190,160,60));
    refreshName->setText(tr("数据手动更新"));


    cityValue = new ShowLabel(this);
    cityValue->addFontSize(2);
    cityValue->setAlignment(Qt::AlignHCenter);
    cityValue->setGeometry(QRect(550,y+30,160,60));
    cityValue->setText(tr("--N--"));
    connect(cityValue,SIGNAL(clicked()),this,SLOT(goSetCity()) );

    netValue = new ShowLabel(this);
    netValue->addFontSize(2);
    netValue->setAlignment(Qt::AlignHCenter);
    netValue->setGeometry(QRect(590,y+110,50,60));
    netValue->setText(tr("--N--"));
    connect(netValue,SIGNAL(clicked()),this,SLOT(goSetTimer()) );

    hourChar = new ShowLabel(this);
    hourChar->addFontSize(2);
    hourChar->setAlignment(Qt::AlignHCenter);
    hourChar->setGeometry(QRect(630,y+110,60,60));
    hourChar->setText(tr("小时"));
    connect(hourChar,SIGNAL(clicked()),this,SLOT(goSetTimer()) );

    rightCity = new QLabel(this);
    rightCity->setAlignment(Qt::AlignHCenter);
    rightCity->setGeometry(QRect(690, y+25, 30, 30));
    rightCity->setPixmap(QPixmap(":/images/right_icon.png"));
    rightCity->setScaledContents(true);

    rightNet = new QLabel(this);
    rightNet->setAlignment(Qt::AlignHCenter);
    rightNet->setGeometry(QRect(690, y+105, 30, 30));
    rightNet->setPixmap(QPixmap(":/images/right_icon.png"));
    rightNet->setScaledContents(true);

    //rightNet->installEventFilter(this);

    refreshBut = new Button(this);
    refreshBut->setButtonPicture(QPixmap( ":/images/refresh_1.png"));
    refreshBut->setPressPicture(QPixmap( ":/images/refresh_2.png"));
    refreshBut->setReleasePicture(QPixmap( ":/images/refresh_1.png"));
    refreshBut->setEnterPicture(QPixmap( ":/images/refresh_2.png"));
    refreshBut->setReleasePicture(QPixmap( ":/images/refresh_1.png"));
    refreshBut->set_X_Y_width_height(580,y+180,130,50);

    connect(refreshBut,SIGNAL(clicked()),this,SLOT(goRefresh()) );

    falgRefresh = 0;

    //显示按钮
    saveBut = new Button(this);
    saveBut->setButtonPicture(QPixmap( ":/images/save_1.png"));
    saveBut->setPressPicture(QPixmap( ":/images/save_2.png"));
    saveBut->setReleasePicture(QPixmap( ":/images/save_1.png"));
    saveBut->setEnterPicture(QPixmap( ":/images/save_2.png"));
    saveBut->setReleasePicture(QPixmap( ":/images/save_1.png"));
    saveBut->set_X_Y_width_height(410,380,130,50);
    connect(saveBut,SIGNAL(clicked()),this,SLOT(goSave()) );

    myProcess = new QProcess(parent);
}

void SysSet::setSysInit(QString city,QString timer)
{
    cityValue->setText(city);
    netValue->setText(timer);
}

int SysSet::getFlagVal()
{
    return falgRefresh;
}

void SysSet::setFlagVal(int val)
{
    falgRefresh = val;
}

void SysSet::goSave()
{
    QString oldCode;
    QFile f("/home/pi/code_file");
    if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "Open failed." << endl;
    }else{
        QTextStream fStream(&f);
        oldCode =  fStream.readLine();
        f.close();
    }


    writeFile("/home/pi/code_file",cityCode);
    writeFile("/home/pi/timer_file",netValue->text());
    emit hourChange(netValue->text().toInt());

    if(oldCode != cityCode){
        QString program = "/usr/bin/python";
        QStringList arguments;
        arguments << "/home/pi/temp_p2_7.py" ;
        myProcess->execute(program, arguments);
        falgRefresh = 1;
        qDebug() << oldCode << cityCode << endl;
    }
}


int SysSet::writeFile(QString fileName, QString data)
{
    QFile f(fileName);

    if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qDebug() << "Open failed." << endl;
        return -1;
    }

    QTextStream fStream(&f);
    fStream << data;
    f.close();
    return 0;
}


void SysSet::goSetCity()
{
    CitySetDlg * setCity = new CitySetDlg(NULL);
    setCity->setGeometry(QRect(160, 70, 600, 300));


    setCity->setWindowModality(Qt::ApplicationModal);
    setCity->setItem(cityValue,&cityCode);
    setCity->show();
}

void SysSet::goSetTimer()
{
    TimerSetDlg * setTimer = new TimerSetDlg(NULL);
    setTimer->setGeometry(QRect(160, 70, 600, 300));

    setTimer->setWindowModality(Qt::ApplicationModal);
    setTimer->setItem(netValue);
    setTimer->show();
}

int SysSet::getNetTimer()
{
    return netValue->text().toInt();
}



bool SysSet::eventFilter(QObject *obj, QEvent *event)
{

}



void SysSet::goRefresh()
{
    QString program = "/usr/bin/python";
    QStringList arguments;
    arguments << "/home/pi/temp_p2_7.py" ;
    myProcess->execute(program, arguments);
    falgRefresh = 1;
}
